package com.qf.day3;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @date 2022/3/26
 * @desc   response 代表响应对象 可以写数据到客户端
 */
@WebServlet("/response1")
public class Response1Demo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //resp.setCharacterEncoding("utf-8");   // 设置 response 对象的编码
        //resp.setHeader("content-type","text/html;charset=utf-8");   // 通过请求头 告诉浏览器该怎么解析发过去的数据

        resp.setContentType("text/html;charset=utf-8");  // 上面两行 等价于 这一行
        //String data="hello response";
        String data="祖国";

        // resp 得到流对象 这个流对象的目的地是 浏览器客户端
        PrintWriter writer = resp.getWriter();
        writer.println(data);
        //writer.print(data);

    }


}
